package com.jiexin.controller;

import com.google.common.collect.Sets;
import com.jiexin.beans.Mail;
import com.jiexin.model.SysUser;
import com.jiexin.service.SysUserService;
import com.jiexin.util.MD5Util;
import com.jiexin.util.MailUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.management.relation.RelationType;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Set;

/**
 * @Author: Jay_Liu
 * @Description:
 * @Date: Created in 12:59 2018/4/6 0006
 * @Modified By:
 */
@Controller
public class UserController {

    @Resource
    private SysUserService sysUserService;

    @RequestMapping("/logout.page")
    public void logout(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //移除session
        request.getSession().invalidate();
        String path = request.getContextPath() + "/signin.jsp";
        response.sendRedirect(path);
    }

    @RequestMapping("/login.page")
    public void login(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        SysUser sysUser = sysUserService.findByKeyWord(username);
        String errorMsg = "";
        //用户点击触发某请求时，无权限时或其他情况系统自动跳转到登陆页面
        //记录此请求，登陆验证通过后，跳转到原请求路径
        String ret = request.getParameter("ret");
        if (StringUtils.isBlank(username)) {
            errorMsg = "用户不可以为空";
        } else if (StringUtils.isBlank(password)) {
            errorMsg = "密码不可以为空";
        } else if (sysUser == null) {
            errorMsg = "查询不到指定用户";
        } else if (!sysUser.getPassword().equals(MD5Util.encrypt(password))) {
            errorMsg = "用户名或密码错误";
        } else if (sysUser.getStatus() != 1) { //状态
            errorMsg = "用户已被冻结，请联系管理员";
        } else {
            //用户验证成功处理
            request.getSession().setAttribute("user", sysUser);
            if (StringUtils.isNotBlank(ret)) {
                //跳转回原请求地址
//                Set<String> set = Sets.newHashSet();
//                set.add("Jay_liu@jiexininfo.com");
//                MailUtil.send(Mail.builder().receivers(set).message("测试").subject("测试").build());
                response.sendRedirect(ret);
                return;
            } else {
//                Set<String> set = Sets.newHashSet();
//                set.add("Jay_liu@jiexininfo.com");
//                MailUtil.send(Mail.builder().receivers(set).message("测试").subject("测试").build());
                response.sendRedirect("/permission/admin/index.page");
                //request.getRequestDispatcher("/admin/index.page").forward(request, response);
                return;
            }
        }

        //用户无法登陆，将错误信息写到界面
        request.setAttribute("error", errorMsg);
        request.setAttribute("username", username);
        if(StringUtils.isNotBlank(ret)) {
            request.setAttribute("ret", ret);
        }
        //客户端跳转链接,登陆页面
        String path = request.getContextPath() + "/signin.jsp";
        request.getRequestDispatcher(path).forward(request, response);
    }
}
